package com.briup.estore.dao.impl;

import com.briup.estore.common.constant.ExceptionMsgEnum;
import com.briup.estore.common.exception.EstoreException;
import com.briup.estore.common.pojo.Book;
import com.briup.estore.common.utils.JDBCUtils;
import com.briup.estore.dao.BookDao;

import java.util.List;

/**
 * @Author briup-adam
 * @Date 2023/8/3 下午3:00
 * @Description 对表【es_book】的数据库操作
 **/

public class BookDaoImpl implements BookDao {
    @Override
    public Book selectById(Integer id) {
        Book book = JDBCUtils.findOne("select id ,name,cover, description,author,publisher, price,store_num,status, category_id from es_book where id = ?;", Book.class, id);
        return book;
    }

    @Override
    public List<Book> selectBooksBefore20() {
        List<Book> list = JDBCUtils.findMany("select id ,name,cover, description,author,publisher, price,store_num,status, category_id from es_book limit 20;", Book.class);
        return list;
    }

    @Override
    public List<Book> selectByCategoryId(Integer categoryId) {
            List<Book> list = JDBCUtils.findMany("select id,name,cover,description,author,publisher,price,store_num,status,category_id from es_book where category_id = ? and status =0;", Book.class, categoryId);
            return  list;


    }

    @Override
    public List<Book> findByQuery(String query) {
            List<Book> list = JDBCUtils.findMany("SELECT id,NAME,cover,description,author,publisher,price,store_num,STATUS,category_id FROM es_book  WHERE NAME LIKE ?  OR author LIKE ?  OR publisher LIKE ?  AND STATUS = 0;", Book.class, "%" + query + "%", "%" + query + "%", "%" + query + "%");
            return  list;
    }

    @Override
    public void updateStoreNumById(Integer num, Integer id) {
        JDBCUtils.DML(" update es_book set store_num =(store_num+(?)) where id=?;",num,id);
    }
}
